@extends('admin/layout/master')
@section('title', '栏目添加')
@section('title2', '栏目添加')

@section('action')
  <a class="layui-btn layui-btn-small layui-btn-normal usersAdd_btn" href="{{ url('admin/type/index') }}"><i class="layui-icon"></i>栏目列表</a>
	<!-- <a class="layui-btn layui-btn-small layui-btn-danger batchDel"><i class="layui-icon"></i>导出数据</a> -->
@endsection

@section('content')
<div class="error">
    @if (count($errors) > 0)
        <div class="alert alert-danger">
            错误列表：
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif
</div>

<form class="layui-form" name="mainform" action="{{ url('/admin/type/store') }}" method="post">
  <div class="layui-form-item">
    <label class="layui-form-label">栏目名</label>
    <div class="layui-input-block">
      <input type="text" name="name" required  lay-verify="required" placeholder="请输入栏目名" value="{{ old('name') }}" autocomplete="off" class="layui-input">
    </div>
  </div>
	<div class="layui-form-item">
    <label class="layui-form-label">上级栏目</label>
    <div class="layui-input-block">
      <select name="parent_id" lay-verify="required">
        <option value="">请选择</option>
        <option value="0">顶级栏目</option>
				@foreach($data as $v)
        <option value="{{ $v['id'] }}">{{ str_repeat("|________", $v['level']) }}{{ $v['name'] }}</option>
				@endforeach
      </select>
    </div>
  </div>
	<div class="layui-form-item">
	  <label class="layui-form-label">栏目图片</label>
	  <div class="layui-input-block">
			<button type="button" class="layui-btn" id="test1">
				<i class="layui-icon">&#xe67c;</i>上传图片
			</button>
			<div style="float: left;width: 50px;height: 50px;margin-right: 20px;border: 1px solid #999;">
				<img style="width: 50px;height: 50px;" id="img" src="/admin/images/face.jpg" alt="">
			</div>
			<input type="hidden" name="img" value="">
	  </div>
	</div>
	<div class="layui-form-item">
	  <label class="layui-form-label">关键字</label>
	  <div class="layui-input-block">
	    <input type="text" name="keywords" placeholder="请输入关键字" value="{{ old('keywords') }}" autocomplete="off" class="layui-input">
	  </div>
	</div>
	<div class="layui-form-item layui-form-text">
    <label class="layui-form-label">描述</label>
    <div class="layui-input-block">
      <textarea name="description" placeholder="描述" class="layui-textarea"></textarea>
    </div>
  </div>
	<div class="layui-form-item">
	  <label class="layui-form-label">导航显示</label>
	  <div class="layui-input-block">
	    <input type="radio" name="is_show" value="1" title="显示" checked>
	    <input type="radio" name="is_show" value="2" title="隐藏">
	  </div>
	</div>
	<div class="layui-form-item">
	  <label class="layui-form-label">排序</label>
	  <div class="layui-input-block">
	    <input type="text" name="weight" placeholder="越大越前" value="{{ old('weight') }}" autocomplete="off" class="layui-input">
	  </div>
	</div>
  <div class="layui-form-item">
    <div class="layui-input-block">
      {{ csrf_field() }}
      <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
</form>
@endsection

@section('layuijs')
<script>
//一般直接写在一个js文件中
layui.use(['layer', 'form', 'upload'], function(){
var layer = layui.layer
,form = layui.form;
var upload = layui.upload;

	//执行实例
	var tag_token = $("[name='_token']").val();
  var uploadInst = upload.render({
    elem: '#test1' //绑定元素
		,data: {'_token':tag_token}
		,field: 'typeimg'
	  {{--,url: "{{url('/admin/type/upload')}}" //上传接口--}}
	  ,url: "{{url('/admin/tool/upload')}}" //上传接口
    ,done: function(res){
      //上传完毕回调
			if(res.success){
				console.log(res);
				layer.msg('上传成功');
				$("#img").attr("src",res.url);
				$("input[name='img']").val(res.url);
			} else {
				layer.msg('上传失败');
			}
    }
    ,error: function(){
      //请求异常回调
			layer.msg('上传失败');
    }
  });
});
</script>
@endsection